package com.mbuswidget;

import java.util.ArrayList;
import java.util.Collections;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Messenger;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class MbusWidgetConfigurationSettingsBlacklistedAddRoute extends
		Activity {
	private ListView itemList;
	private Integer widgetID = AppWidgetManager.INVALID_APPWIDGET_ID;
	private MBusWidgetService mbusBoundService;
	private ArrayList<String> blacklistedRoutes;

	private MBusInfo mbusInfo;

	/** Messenger for communicating with service. */
	Messenger mService = null;

	private ServiceConnection mConnection = new ServiceConnection() {
		public void onServiceConnected(ComponentName className, IBinder service) {
			// This is called when the connection with the service has been
			// established, giving us the service object we can use to
			// interact with the service. Because we have bound to a explicit
			// service that we know is running in our own process, we can
			// cast its IBinder to a concrete class and directly access it.
			mbusBoundService = ((MBusWidgetService.LocalBinder) service)
					.getService();
			// mService = new Messenger((MBusWidgetService.LocalBinder)service);

			// Tell the user about this for our demo.
			// Toast.makeText(MBusWidgetConfigurationStopsAddStopByRoute.this,
			// R.string.local_service_connected, Toast.LENGTH_SHORT)
			// .show();

			ShowResults();
		}

		public void onServiceDisconnected(ComponentName className) {
			// This is called when the connection with the service has been
			// unexpectedly disconnected -- that is, its process crashed.
			// Because it is running in our same process, we should never
			// see this happen.
			mbusBoundService = null;

			// This is called when the connection with the service has been
			// unexpectedly disconnected -- that is, its process crashed.
			mService = null;

			// Toast.makeText(MBusWidgetConfigurationStopsAddStopByRoute.this,
			// R.string.local_service_disconnected, Toast.LENGTH_SHORT)
			// .show();
		}
	};
	private boolean mIsBound;

	void doBindService() {
		// Establish a connection with the service. We use an explicit
		// class name because we want a specific service implementation that
		// we know will be running in our own process (and thus won't be
		// supporting component replacement by other applications).
		bindService(new Intent(
				MbusWidgetConfigurationSettingsBlacklistedAddRoute.this,
				MBusWidgetService.class), mConnection, Context.BIND_AUTO_CREATE);
		mIsBound = true;
	}

	void doUnbindService() {
		if (mIsBound) {
			// Detach our existing connection.
			unbindService(mConnection);
			mIsBound = false;
		}
	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.selectstopby);
		setResult(RESULT_CANCELED);
		
		widgetID = getIntent().getExtras().getInt(
				AppWidgetManager.EXTRA_APPWIDGET_ID,
				AppWidgetManager.INVALID_APPWIDGET_ID);

		if (widgetID == AppWidgetManager.INVALID_APPWIDGET_ID)
			finish();

		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(this);

		itemList = (ListView) findViewById(R.id.itemList);
		
		blacklistedRoutes = MBusWidgetConstants.GetWidgetBlacklist(prefs, widgetID);
		
		doBindService();

	}

	public void ShowResults() {
		mbusInfo = mbusBoundService.getBusInfo();
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1);
		itemList.setAdapter(adapter);
		for (int i = 0; i < mbusInfo.getRoutes().size(); i++) {
			String routeName = mbusInfo.getRoutes().get(i).getName();
			if(Collections.binarySearch(blacklistedRoutes, routeName) < 0) {
				adapter.add(routeName);
			}
		}
		
		itemList.setOnItemClickListener(new OnItemClickListener() {
			// @Override
			public void onItemClick(AdapterView<?> a, View v, int position,
					long id) {
				String[] routeToAdd = new String[1];
				routeToAdd[0] = itemList.getAdapter().getItem(position).toString();
				
				Intent intent = new Intent(MbusWidgetConfigurationSettingsBlacklistedAddRoute.this, MbusWidgetConfigurationSettingsBlacklisted.class);
				intent.putExtra(MBusWidgetConstants.BLACKLISTED_ROUTE_ADD, routeToAdd);
				intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetID);
				setResult(RESULT_OK, intent);
				doUnbindService();
				finish();
			}
		});
	}
}
